package seGobangClient.uiSystem3D;
import java.util.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.*;
import com.sun.j3d.loaders.*;
import com.sun.j3d.loaders.objectfile.*;
import javax.vecmath.*;
import seGobangResources.*;


/**
 * UI3D������ά�������������
 * @author plus
 *
 */
public class ObjectFactory {
	private static java.awt.Panel PanelTexture=new java.awt.Panel();	//�����������
	private static Geometry GeoChess=createChessGeometry();				//Ԥ������ģ��
	/**
	 * ����������
	 * @return ������
	 */
	public static Group createChessboard(){
		Appearance AppSurface=new Appearance(), AppBody=new Appearance();
		try{
                    LoadTextureToAppearance(AppSurface,PathRes.getPath("Texture/Surface"));
                    LoadTextureToAppearance(AppBody,PathRes.getPath("Texture/Wood"));
                }catch(Exception e){
                    e.printStackTrace();
                }
		SetMaterialToAppearance(AppSurface);
		SetMaterialToAppearance(AppBody);
		
		Transform3D transBody=new Transform3D();
		transBody.set(new Vector3f(0f,-0.55f,0f));
		TransformGroup TGBody=new TransformGroup(transBody);
		Box CBSurface=new Box(7.5f,-0.05f,7.5f,
				Primitive.GENERATE_NORMALS|
				Primitive.GENERATE_TEXTURE_COORDS|
				Primitive.ENABLE_GEOMETRY_PICKING,
				AppSurface);
		Box CBBody=new Box(7.5f,0.45f,7.5f,
				Primitive.GENERATE_NORMALS|
				Primitive.GENERATE_TEXTURE_COORDS,
				AppBody);
                CBSurface.setCapability(Box.ALLOW_PICKABLE_WRITE);
		CBBody.setPickable(false);
		TGBody.addChild(CBBody);
		Group gp=new Group();
		gp.addChild(CBSurface);
		gp.addChild(TGBody);
		return gp;
	}

	public static Group createChessIndicator(){
            Group gpCI=null;
            try{
		gpCI= LoadSimpleObjectFromFile(PathRes.getPath("Model/Indicator"));
            }catch(Exception e){
                e.printStackTrace();
            }
		Appearance AppCI=new Appearance();
		AppCI.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
		SetMaterialToAppearance(AppCI,new Color3f(1f,1f,1f),new Color3f(1f,1f,1f),new Color3f(1f,1f,1f),0);
		AppCI.setTransparencyAttributes(new TransparencyAttributes
				(TransparencyAttributes.NICEST,0.3f));
		
		Shape3D shapeCI=(Shape3D)gpCI.getChild(0);
		shapeCI.setAppearance(AppCI);
		return gpCI;
	}
        
        
	public static Group createChessTG(Color3f color,float x, float y, float z){
		Appearance AppChess=new Appearance();
		AppChess.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
		SetMaterialToAppearance(AppChess,color,color,new Color3f(1f,1f,1f),5);
                Transform3D trans=new Transform3D();
                    trans.set(new Vector3f(x,y,z));
		TransformGroup TGChess=new TransformGroup(trans);
		TGChess.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		TGChess.addChild(new Shape3D(GeoChess,AppChess));
                TGChess.setPickable(false);
		return TGChess;
	}
	
	public static Shape3D createPureColorSurface(float width,float height,Color3f color, float alpha){
		return new Plane2D(width,height,color,alpha,null);
	}
	
	public static Shape3D createTexturedSurface(float width,float height,Color3f color, float alpha,String fName){
		TextureLoader texLoader=new TextureLoader(fName,"RGBA", PanelTexture);
		Plane2D res = new Plane2D(width,height,color,alpha,texLoader.getTexture());
                res.setUserData(fName);
                return res;
	}
	
        public static Shape3D createTexturedSurface(float width,float height,Color3f color, float alpha,String fName,String flag){
		TextureLoader texLoader=new TextureLoader(fName,"RGBA", PanelTexture);
		Plane2D res = new Plane2D(width,height,color,alpha,texLoader.getTexture());
                res.setUserData(flag);
                return res;
	}
        
	private static Geometry createChessGeometry(){
		Group ChessGroup=null;
                try{
                    ChessGroup=LoadSimpleObjectFromFile(PathRes.getPath("Model/Chess"));
                }catch(Exception e){
                    e.printStackTrace();
                }
		Shape3D shapeChess=(Shape3D) ChessGroup.getChild(0);
                
		return shapeChess.getGeometry();
		
	}
	
	public static Group createRoom(){
            Group gpRoom=null;
            try{
		gpRoom= LoadSimpleObjectFromFile(PathRes.getPath("Model/Room"));
            }catch(Exception e){
                e.printStackTrace();
            }
		return gpRoom;
	}
        
        public static Group createObject(String Key){
            Group gpRoom=null;
            try{
		gpRoom= LoadSimpleObjectFromFile(PathRes.getPath(Key));
            }catch(Exception e){
                e.printStackTrace();
            }
            gpRoom.setPickable(false);
		return gpRoom;
	}

	private static void LoadTextureToAppearance(Appearance app,String fName){
		TextureLoader texLoader=new TextureLoader(fName,PanelTexture);
		app.setTexture(texLoader.getTexture());
	}

	private static void SetMaterialToAppearance(Appearance app){
		Material mat=new Material();
		app.setMaterial(mat);
	}
	private static void SetMaterialToAppearance(Appearance app,Color3f ambientColor,Color3f diffuseColor,
			Color3f specularColor,float shininess){
		Material mat=new Material();
		mat.setCapability(Material.ALLOW_COMPONENT_WRITE);
		mat.setAmbientColor(ambientColor);
		mat.setDiffuseColor(diffuseColor);
		mat.setSpecularColor(specularColor);
		mat.setShininess(shininess);
		app.setMaterial(mat);
	}
	

	private static Group LoadSimpleObjectFromFile(String fName)
	{
		Scene s=null;
		ObjectFile objFile=new ObjectFile();
		try{
			s=objFile.load(fName);
		}catch(Exception e){
			e.printStackTrace();
		}
		Group obj= s.getSceneGroup();
		Enumeration<Node> sceneChildren=obj.getAllChildren();
		while(sceneChildren.hasMoreElements()){
			Node node=sceneChildren.nextElement();
			node.setPickable(false);
		}
		return obj;
	}
}